﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace KLTN
{
    public class FreeCamera:CCamera
    {
        public float Yaw { get; set; }
        public float Pitch { get; set; }
        public Vector3 Position { get; set; }
        public Vector3 Target { get; set; }
        private Vector3 translation;// khoang cach camera dich chuyen

        public FreeCamera(Vector3 Position, float Yaw, float Pitch, GraphicsDevice graphicDevice)
            : base(graphicDevice)
        {
            this.Position = Position;//vi tri
            this.Yaw = Yaw;          //X
            this.Pitch = Pitch;      //Y
            this.translation = Vector3.Zero;
            Target = Vector3.Zero;
        }
        public void Rotate(float YawChange, float PitchChange)
        {
            this.Yaw += YawChange;
            this.Pitch += PitchChange;
        }
        public void Move(Vector3 Translation)
        {
            this.translation += Translation;
        }
        public override void Update()
        {
            Matrix rotation = Matrix.CreateFromYawPitchRoll(Yaw, Pitch, 0);//tao ra 1 ma tran xoay voi Yaw Pitch Roll
            //Update lai Position va Reset lai translation(khoang dich chuyen)

            if (translation!=Vector3.Zero)
            {
                translation = Vector3.Transform(translation, rotation);
            }
            
            Position += translation;//update vi tri
            translation = Vector3.Zero;//reset

            Vector3 forward = Vector3.Transform(Vector3.Forward, rotation);

            Target = Position + forward;

            Vector3 up = Vector3.Transform(Vector3.Up,rotation);

            View = Matrix.CreateLookAt(Position,Target,up);
        }

    }
}
